<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016-05-19
 * Time: 13:52
 */


namespace app\controllers;

use app\lib\Tools;
use app\lib\WXBaseController;
use app\models\WxGoodDao;
use Yii;
use app\lib\WebService;
use yii\web\User;
use app\models\ApiResponseStatusEnum;

class ApiController extends WXBaseController
{
    public function beforeAction($action)
    {
//        return true;
        return $this->_auth();
    }

    /**
     * 设置单品订单状态
     * @return string
     */
    public function actionSetGoodsOrderStatus()
    {
        $request = file_get_contents("php://input");
        $request = json_decode($request);

        $order_code = $request->order_code;
        $status = $request->status;
        $result = 0;
        if ($order_code == null || $status == null) {
            return $this->_response(ApiResponseStatusEnum::ParamsError());
        } else {
            $result = WxGoodDao::setOrderStatusByCode($order_code, $status);
        }
        if ($result > 0) {
            return $this->_response(ApiResponseStatusEnum::Success());
        }

        return $this->_response(ApiResponseStatusEnum::OtherError());
    }

    /**
     * 单品订单配送状态
     * @return string
     */
    public function actionSetGoodsOrderDelivery()
    {
        $request = file_get_contents("php://input");
        $request = json_decode($request);

        $order_code = $request->order_code;
        //修改单品订单状态
        $result = WxGoodDao::setOrderStatusByCode($order_code, 11);

        $order_delivery_code = $request->order_delivery_code;
        $goods_list = $request->goods_list;
        $logistics_company = $request->logistics_company;
        $logistics_code = $request->logistics_code;
        $goods_list_str = '';
        foreach ($goods_list as $item) {
            $goods_list_str = $goods_list_str . $item->goods_code . ',' . $item->quantity . ';';
        }

        if ($order_code == null || $order_delivery_code == null || $logistics_company == null || $logistics_code == null || $goods_list_str == '') {
            return $this->_response(ApiResponseStatusEnum::ParamsError());
        }

        $out_orderdelivery_id = WxGoodDao::setGoodsOrderDelivery($order_code, $order_delivery_code, $logistics_company, $logistics_code, $goods_list_str);

        if ($out_orderdelivery_id != '0') {
            return $this->_response(ApiResponseStatusEnum::Success());
        }

        return $this->_response(ApiResponseStatusEnum::OtherError());
    }

    /**
     * 接口签名验证
     * @return bool
     */
    private function _auth()
    {
        // 缺少account参数
        $accout = \Yii::$app->request->get('account');
        if ($accout == null) {
            echo $this->_response(ApiResponseStatusEnum::AccountError());
            return false;
        }

        // 缺少timestamp参数
        $timestamp = \Yii::$app->request->get('timestamp');
        if ($timestamp == null) {
            echo $this->_response(ApiResponseStatusEnum::TimestampError());
            return false;
        }

        // 缺少sign参数
        $sign = \Yii::$app->request->get('sign');
        if ($sign == null) {
            echo $this->_response(ApiResponseStatusEnum::SignError());
            return false;
        }

        // timestamp过期
        if (time() - $timestamp > \Yii::$app->params['api.TimestampValidity']) {
            echo $this->_response(ApiResponseStatusEnum::TimestampError());
            return false;
        }

        // sign认证
        $token = $this->_getAccountToken($accout);
        $authSign = hash_hmac('sha256', $accout . $token . $timestamp, $token);
        if ($authSign != $sign) {
            echo $this->_response(ApiResponseStatusEnum::SignError());
            return false;
        }

        return true;
    }

    /**
     * 获取account的token
     * @param string $account
     * @return string
     */
    private function _getAccountToken($account)
    {
        $key = 'api_account_token_' . $account;
        $token = Yii::$app->params[$key];
        return $token;
    }

    /**
     * 调用返回
     * @param ApiResponseStatus $responseStatus
     * @param null $data
     * @return string
     */
    private function _response($responseStatus, $data = null)
    {
        $result = [
            'status' => $responseStatus->status,
            'message' => $responseStatus->message,
            'data' => $data
        ];

        return json_encode($result, true);
    }
}